package ar.com.fiuba.ElectronicCircuitAnalyst.application.impl;

import java.util.List;

import ar.com.fiuba.ElectronicCircuitAnalyst.application.api.SimulationService;
import ar.com.fiuba.ElectronicCircuitAnalyst.application.api.dto.SimulationResultDto;
import ar.com.fiuba.ElectronicCircuitAnalyst.application.api.dto.SimulationValuesDto;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Circuit;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.repository.CircuitRepository;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.CircuitElementNotFoundException;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.CircuitInputValuesException;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.SimulationException;

public class SimulationServiceImpl implements SimulationService {

	private CircuitRepository circuitRepository;

	public SimulationServiceImpl(CircuitRepository circuitRepository) {
		this.circuitRepository = circuitRepository;
	}

	public SimulationResultDto simulate(String circuitName, SimulationValuesDto simulationValues)
			throws CircuitElementNotFoundException, CircuitInputValuesException, SimulationException {
		SimulationResultDto simulationResult = new SimulationResultDto();
		Circuit circuit = circuitRepository.getCircuitByName(circuitName);

		for (List<Boolean> clockTick : simulationValues.getValues()) {
			circuit.setInputValues(clockTick);
			circuit.simulate();
			simulationResult.addResult(circuit.getOutputValues());
		}

		return simulationResult;
	}

}
